<?php
namespace app\adminmall\controller;
use app\adminmall\model\ConsMemberModel;
use app\common\controller\AdminBaseController;
use think\Config;
use think\Db;

/**
 * 消费用户管理
 * Class Member
 * @package app\admin\controller
 */
class ConsMemberController extends AdminBaseController {
    protected $consmember_model;

    protected function _initialize() {
        parent::_initialize();
        $this->consmember_model = new ConsMemberModel();
    }

    /**
     * 用户管理
     * @param int    $page
     * @return mixed
     */
    public function index($page = 1) {
    	$map = [];
		$field = 'm.*,g.id,g.mobile,g.real_name,g.city,b.store_name';		    	
    	if(request()->isPost()){
		 	$param = input('post.');
			//用户名称
			if($param['realname']){
				$map['g.real_name'] = ['like', '%'.$param['realname'].'%'];
			}
			//手机号码
			if($param['mobile']){
				$map['g.mobile'] = ['like', '%'.$param['mobile'].'%'];
			}
			//商家名称
			if($param['store_name']){
				$map['b.store_name'] = ['like', '%'.$param['store_name'].'%'];
			}
			//时间搜索
			if($param['add_time'] && $param['end_time']){
				$map['m.time'] = ['between time', [$param['add_time'],$param['end_time']]];
			}			 				
	 	}
	 	
		$list=Db::name("mall_consmember")->alias('m')->join('__MEMBER__ g','g.id=m.u_id')->join('mall_agent b','m.agent_id=b.id')->field($field)->where($map)->order("m.time desc")->paginate(10);
        
        return $this->fetch('index', ['list' => $list]);
    }
	
	/**
     * [export导出]
     * @author [longx] 
     */
    public function export(){
    	$map = [];
		$field = 'm.*,g.id,g.mobile,g.real_name,g.city,b.store_name';		    	
    	if(request()->isPost()){
		 	$param = input('post.');
			//用户名称
			if($param['realname']){
				$map['g.real_name'] = ['like', '%'.$param['realname'].'%'];
			}
			//手机号码
			if($param['mobile']){
				$map['g.mobile'] = ['like', '%'.$param['mobile'].'%'];
			}
			//商家名称
			if($param['store_name']){
				$map['b.store_name'] = ['like', '%'.$param['store_name'].'%'];
			}
			//时间搜索
			if($param['add_time'] && $param['end_time']){
				$map['m.time'] = ['between time', [$param['add_time'],$param['end_time']]];
			}			 				
	 	}
	 	
		$list=Db::name("mall_consmember")->alias('m')
		->join('__MEMBER__ g','g.id=m.u_id')
		->join('mall_agent b','m.agent_id=b.id')
		->field($field)->where($map)
		->order("m.time desc")
		->paginate(100);
		
        $str = "用户名,手机号码,商家名称,积分,添加时间,\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
            $real_name = iconv('utf-8','gbk',$row['real_name']); //中文转码 			
			$mobile = iconv('utf-8','gbk',$row['mobile']);
			$store_name = iconv('utf-8','gbk',$row['store_name']);
			$score = iconv('utf-8','gbk',$row['score']);
			$time = iconv('utf-8','gbk',$row['time']);	
						                           
            $str .= $real_name.",".$mobile.",".$store_name.",".$score.",".$time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='消费会员列表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
     }
}